package com.ruoyi.mall.service;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mall.domain.MallGoodsNorm;
import com.ruoyi.mall.mapper.MallGoodsNormMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 商品规格Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-11
 */
@Service
public class MallGoodsNormService {
    @Autowired
    private MallGoodsNormMapper mallGoodsNormMapper;

    /**
     * 查询商品规格
     *
     * @param normId 商品规格主键
     * @return 商品规格
     */
    public MallGoodsNorm selectMallGoodsNormByNormId(Long normId) {
        return mallGoodsNormMapper.selectMallGoodsNormByNormId(normId);
    }

    /**
     * 查询商品规格列表
     *
     * @param mallGoodsNorm 商品规格
     * @return 商品规格
     */
    public List<MallGoodsNorm> selectMallGoodsNormList(MallGoodsNorm mallGoodsNorm) {
        return mallGoodsNormMapper.selectMallGoodsNormList(mallGoodsNorm);
    }

    /**
     * 新增商品规格
     *
     * @param mallGoodsNorm 商品规格
     * @return 结果
     */
    public int insertMallGoodsNorm(MallGoodsNorm mallGoodsNorm) {
        mallGoodsNorm.setCreateTime(DateUtils.getNowDate());
        return mallGoodsNormMapper.insertMallGoodsNorm(mallGoodsNorm);
    }

    /**
     * 修改商品规格
     *
     * @param mallGoodsNorm 商品规格
     * @return 结果
     */
    public int updateMallGoodsNorm(MallGoodsNorm mallGoodsNorm) {
        mallGoodsNorm.setUpdateTime(DateUtils.getNowDate());
        return mallGoodsNormMapper.updateMallGoodsNorm(mallGoodsNorm);
    }

    @Transactional(rollbackFor = Exception.class)
    public int saveGoodsNorm(List<MallGoodsNorm> normList) {
        // 取产品ID
        Long goodsId = normList.get(0).getGoodsId();
        Map<Long, MallGoodsNorm> normMap = normList.stream().filter(norm -> norm.getNormId() != null).collect(Collectors.toMap(MallGoodsNorm::getNormId, Function.identity(), (o, n) -> o));
        // 取保存数据存在的normIdList
        List<Long> normIdList = normList.stream().filter(mallGoodsNorm -> mallGoodsNorm.getNormId() != null).map(MallGoodsNorm::getNormId).collect(Collectors.toList());
        // 查询数据库中存在的norm列表
        MallGoodsNorm param = new MallGoodsNorm();
        param.setGoodsId(goodsId);
        List<MallGoodsNorm> dbNormList = mallGoodsNormMapper.selectMallGoodsNormList(param);
        // 删除传参不存在的规格列表
        List<Long> waitDelList = dbNormList.stream().filter(norm -> !normIdList.contains(norm.getNormId())).map(MallGoodsNorm::getNormId).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(waitDelList)) {
            mallGoodsNormMapper.deleteMallGoodsNormByNormIds(waitDelList.toArray(new Long[0]));
        }
        // 修改前端传值的规格列表
        List<MallGoodsNorm> waitModifyList = dbNormList.stream().filter(norm -> normIdList.contains(norm.getNormId())).collect(Collectors.toList());
        int res = 0;
        for (MallGoodsNorm norm : waitModifyList) {
            MallGoodsNorm modifyNorm = normMap.get(norm.getNormId());
            if (modifyNorm != null) {
                modifyNorm.setUpdateBy(SecurityUtils.getUsername());
                modifyNorm.setUpdateTime(new Date());
                int cnt = mallGoodsNormMapper.updateMallGoodsNorm(modifyNorm);
                if (cnt <= 0) {
                    throw new ServiceException("修改规格失败");
                }
                res += cnt;
            }
        }
        // 新增前端传参无normId的集合
        List<MallGoodsNorm> waitAddList = normList.stream().filter(norm -> norm.getNormId() == null).collect(Collectors.toList());
        for (MallGoodsNorm norm : waitAddList) {
            norm.setCreateBy(SecurityUtils.getUsername());
            norm.setCreateTime(new Date());
            norm.setDelFlag("0");
            int cnt = mallGoodsNormMapper.insertMallGoodsNorm(norm);
            if (cnt <= 0) {
                throw new ServiceException("修改规格失败");
            }
            res += cnt;
        }
        return res;
    }

    /**
     * 批量删除商品规格
     *
     * @param normIds 需要删除的商品规格主键
     * @return 结果
     */
    public int deleteMallGoodsNormByNormIds(Long[] normIds) {
        return mallGoodsNormMapper.deleteMallGoodsNormByNormIds(normIds);
    }

    /**
     * 删除商品规格信息
     *
     * @param normId 商品规格主键
     * @return 结果
     */
    public int deleteMallGoodsNormByNormId(Long normId) {
        return mallGoodsNormMapper.deleteMallGoodsNormByNormId(normId);
    }
}
